var CryptoJS = require("crypto-js");
export default class WXBizDataCrypt {
  appId: number
  sessionKey: string

  constructor(appId, sessionKey) {
    this.appId = appId
    this.sessionKey = sessionKey
  }

  decryptData(encryptedData, iv) {
    // base64 decode


    var sessionKey = Buffer.from(this.sessionKey).toString('base64')
    encryptedData = Buffer.from(encryptedData).toString('base64')



    iv = Buffer.from(iv, 'base64').toString('base64')

    // console.log('sessionKey',sessionKey);
    // console.log('encryptedData',encryptedData);
    // console.log('iv',iv);

    try {
      // 解密
      console.dir(CryptoJS);

      var decipher = CryptoJS.createDecipheriv('aes-128-cbc', sessionKey, iv)
      console.log(decipher);

      // // 设置自动 padding 为 true，删除填充补位
      // decipher.setAutoPadding(true)
      // var decoded = decipher.update(encryptedData, 'binary', 'utf8')
      // decoded += decipher.final('utf8')

      // decoded = JSON.parse(decoded)

    } catch (err) {
      console.log("解析错误");

      // throw new Error('Illegal Buffer')
    }

    // if (decoded.watermark.appid !== this.appId) {
    //   // throw new Error('Illegal Buffer')
    // }

    // return decoded
  }
}